home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BDA / BDASample / bdasampl.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  11.3 KB  |  386 lines

  1. //------------------------------------------------------------------------------
  2. // File: Bdasampl.cpp
  3. //
  4. // Desc: Sample code implementing BDA graph building.
  5. //
  6. // Copyright (c) 2000-2001, Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9. #include "bdasampl.h"
  10. #include "graph.h"
  11. #include "resource.h"
  12.  
  13. #include <initguid.h>
  14. #include <objbase.h>
  15.  
  16.  
  17. // Globals
  18. HWND                hwndMain;
  19. HWND                g_hwndDlg;
  20. HINSTANCE           hInst;
  21. TCHAR               szAppName[]  = TEXT("BDASampl");
  22. TCHAR               szAppTitle[] = TEXT("BDA Sample");
  23.  
  24. CBDAFilterGraph*    g_pfg = NULL;
  25.  
  26. const int MAJOR_CHANNEL_LOWER_LIMIT = -1;
  27. const int MAJOR_CHANNEL_UPPER_LIMIT = 126;
  28. const int MINOR_CHANNEL_LOWER_LIMIT = -1;
  29. const int MINOR_CHANNEL_UPPER_LIMIT = 126;
  30.  
  31. INT WINAPI
  32. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
  33.     INT nCmdShow)
  34. {
  35.     MSG         msg;
  36.     HWND        hwnd;
  37.     WNDCLASS    wndclass;
  38.     HACCEL      hAccel;
  39.  
  40.     hInst   = hInstance;
  41.  
  42.     //initialize COM library
  43.     HRESULT hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);
  44.     if (FAILED (hr))
  45.     {
  46.         MessageBox(
  47.             NULL, 
  48.             TEXT("Failed to load COM library!"),
  49.             TEXT("Initialization Error"), 
  50.             MB_ICONEXCLAMATION
  51.             );
  52.         return 0;
  53.     }
  54.  
  55.     wndclass.style         = 0; //CS_HREDRAW | CS_VREDRAW;
  56.     wndclass.lpfnWndProc   = WndProc;
  57.     wndclass.cbClsExtra    = 0;
  58.     wndclass.cbWndExtra    = 0;
  59.     wndclass.hInstance     = hInst;
  60.     wndclass.hIcon         = LoadIcon(hInst, TEXT("BDASAMPLICON"));
  61.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  62.     wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
  63.     wndclass.lpszMenuName  = szAppName;
  64.     wndclass.lpszClassName = szAppName;
  65.     RegisterClass(&wndclass);
  66.  
  67.     hwnd = CreateWindow(szAppName, szAppTitle, WS_OVERLAPPEDWINDOW | 
  68.                 WS_CLIPCHILDREN, 200, 200, 500, 280, NULL, NULL, hInst, NULL);
  69.     ASSERT(hwnd);
  70.  
  71.     // Create the BDA filter graph and initialize its components
  72.     g_pfg = new CBDAFilterGraph();
  73.     ASSERT(g_pfg);
  74.  
  75.     // If the graph failed to build, don't go any further.
  76.     if (!g_pfg)
  77.     {
  78.         MessageBox(hwnd, TEXT("Failed to create the filter graph!"),
  79.                    TEXT("Initialization Error"), MB_ICONEXCLAMATION);
  80.         return 0;
  81.     }
  82.     
  83.     ShowWindow(hwnd, nCmdShow);
  84.     hwndMain = hwnd;
  85.  
  86.     hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(ACC_GRAPH));
  87.  
  88.     while(GetMessage(&msg, NULL, 0, 0) > 0)
  89.     {
  90.         if(!TranslateAccelerator(hwnd, hAccel, &msg))
  91.         {
  92.             //if (!IsDialogMessage (hwnd, &msg))
  93.             {
  94.                 TranslateMessage(&msg);
  95.                 DispatchMessage(&msg);
  96.             }
  97.         }
  98.     }
  99.  
  100.     // Release the BDA components and clean up
  101.     delete g_pfg;
  102.  
  103.     CoUninitialize ();
  104.     
  105.     return msg.wParam;
  106. }
  107.  
  108.  
  109. // WndProc                                                                    
  110. LRESULT CALLBACK
  111. WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  112. {
  113.     switch(message)
  114.     {
  115.     case WM_CREATE:
  116.         {
  117.             DbgInitialise (hInst);
  118.             break
  119.             ;
  120.         }
  121.     case WM_SIZE:
  122.         {
  123.             if(g_pfg->m_fGraphBuilt)
  124.                 g_pfg->SetVideoWindow(hwndMain);
  125.             break;
  126.         }
  127.         
  128.     case WM_COMMAND:
  129.         switch(LOWORD(wParam))
  130.         {
  131.         case IDM_BUILD_ATSC:
  132.             {
  133.                 if(FAILED(g_pfg->BuildGraph(ATSC)))
  134.                 {
  135.                     ErrorMessageBox(TEXT("Could not Build the ATSC BDA FilterGraph."));
  136.                 }
  137.                 else
  138.                 {
  139.                     g_pfg->SetVideoWindow(hwndMain);
  140.                 }
  141.         
  142.                 HMENU hMenu = GetSubMenu (GetMenu (hwnd), 1);
  143.  
  144.                 EnableMenuItem (hMenu, IDM_BUILD_ATSC, MF_GRAYED | MF_BYCOMMAND);
  145.  
  146.                 EnableMenuItem (
  147.                     hMenu, 
  148.                     IDM_STOP_GRAPH, 
  149.                     (g_pfg->m_fGraphRunning) ? (MF_BYCOMMAND|MF_ENABLED) : (MF_BYCOMMAND|MF_GRAYED) 
  150.                     );
  151.  
  152.                 EnableMenuItem (
  153.                     hMenu, 
  154.                     IDM_SELECT_CHANNEL, 
  155.                     (g_pfg->m_fGraphBuilt) ? (MF_BYCOMMAND|MF_ENABLED) : (MF_BYCOMMAND|MF_GRAYED) );
  156.                 break;
  157.             }
  158.             
  159.         case IDM_RUN_GRAPH:
  160.             {
  161.                 if(g_pfg->m_fGraphBuilt)
  162.                 {   
  163.                     if(!g_pfg->m_fGraphRunning)
  164.                     {
  165.                         if(FAILED(g_pfg->RunGraph()))
  166.                         {
  167.                             ErrorMessageBox(TEXT("Could not play the FilterGraph."));
  168.                         }
  169.                     }
  170.                 }
  171.                 else
  172.                 {
  173.                     ErrorMessageBox(TEXT("The FilterGraph is not yet built."));
  174.                 }
  175.  
  176.                 break;
  177.             }
  178.             
  179.         case IDM_STOP_GRAPH:
  180.             {
  181.                 if(g_pfg->m_fGraphBuilt)
  182.                 {
  183.                     if(g_pfg->m_fGraphRunning)
  184.                     {
  185.                         if(FAILED(g_pfg->StopGraph()))
  186.                         {
  187.                             ErrorMessageBox(TEXT("Could not stop the FilterGraph,"));
  188.                         }
  189.                     }
  190.                 }
  191.                 else
  192.                 {
  193.                     ErrorMessageBox(TEXT("The FilterGraph is not yet built."));
  194.                 }
  195.                 HMENU hMenu = GetSubMenu (GetMenu (hwnd), 1);
  196.                 EnableMenuItem (
  197.                     hMenu, 
  198.                     IDM_SELECT_CHANNEL, 
  199.                     MF_BYCOMMAND | MF_GRAYED);
  200.                 break;
  201.             }
  202.             
  203.         case IDM_SELECT_CHANNEL:
  204.             {
  205.                 if(g_pfg->m_fGraphBuilt)
  206.                 {
  207.                     g_hwndDlg = reinterpret_cast <HWND> ( DialogBox(
  208.                         hInst, 
  209.                         MAKEINTRESOURCE(IDD_SELECT_CHANNEL),
  210.                         hwnd, 
  211.                         reinterpret_cast<DLGPROC>(SelectChannelDlgProc)
  212.                         ) );
  213.                 }
  214.                 else
  215.                 {
  216.                     ErrorMessageBox(TEXT("The FilterGraph is not yet built."));
  217.                 }
  218.                 break;
  219.             }
  220.             
  221.         case IDM_ABOUT:
  222.             {
  223.                 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, 
  224.                           reinterpret_cast<DLGPROC>(AboutDlgProc));
  225.                 break;
  226.             }
  227.             
  228.         case IDM_EXIT:
  229.             {
  230.                 DbgTerminate();
  231.                 PostQuitMessage(0);
  232.                 break;
  233.             }
  234.             
  235.         default:
  236.             break;
  237.         }
  238.  
  239.     case WM_INITMENU:
  240.         if(g_pfg->m_fGraphFailure)
  241.         {
  242.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC, MF_BYCOMMAND| MF_GRAYED);
  243.             EnableMenuItem((HMENU)wParam, IDM_RUN_GRAPH, MF_BYCOMMAND| MF_GRAYED);
  244.             EnableMenuItem((HMENU)wParam, IDM_STOP_GRAPH, MF_BYCOMMAND| MF_GRAYED);
  245.         }
  246.         else
  247.         {
  248.             EnableMenuItem((HMENU)wParam, IDM_RUN_GRAPH, 
  249.                 g_pfg->m_fGraphRunning ? MF_BYCOMMAND|MF_GRAYED : MF_BYCOMMAND|MF_ENABLED);
  250.  
  251.             EnableMenuItem((HMENU)wParam, IDM_BUILD_ATSC, 
  252.                 g_pfg->m_fGraphBuilt ? MF_BYCOMMAND|MF_GRAYED : MF_BYCOMMAND|MF_ENABLED);
  253.         
  254.             // we can stop viewing if it's currently viewing
  255.             EnableMenuItem((HMENU)wParam, IDM_STOP_GRAPH, 
  256.                 (g_pfg->m_fGraphRunning) ? MF_BYCOMMAND|MF_ENABLED : MF_BYCOMMAND|MF_GRAYED);
  257.         }
  258.         EnableMenuItem(
  259.             (HMENU)wParam, 
  260.             IDM_SELECT_CHANNEL, 
  261.             g_pfg->m_fGraphBuilt ? MF_BYCOMMAND|MF_ENABLED : MF_BYCOMMAND|MF_GRAYED);
  262.  
  263.         break;
  264.  
  265.     case WM_CLOSE:
  266.     case WM_DESTROY:
  267.         DbgTerminate();
  268.         PostQuitMessage(0);
  269.         break;
  270.         
  271.     default:
  272.         break;
  273.     }
  274.  
  275.     return DefWindowProc(hwnd, message, wParam, lParam);
  276. }
  277.  
  278.  
  279. // AboutDlgProc
  280. //
  281. // Dialog Procedure for the "about" dialog box.
  282. //
  283. BOOL CALLBACK 
  284. AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  285. {
  286.     switch(msg) 
  287.     {
  288.         case WM_COMMAND:
  289.             EndDialog(hwnd, TRUE);
  290.             return TRUE;
  291.     
  292.         case WM_INITDIALOG:
  293.             return TRUE;
  294.     }
  295.     return FALSE;
  296. }
  297.  
  298.  
  299. // SelectChannelDlgProc
  300. // Dialog Procedure for the "Select Channel" dialog box.
  301. //                                                                              
  302. BOOL CALLBACK
  303. SelectChannelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  304. {
  305.     LONG    lChannelMaj  = g_pfg->GetMajorChannel ();
  306.     LONG    lChannelMin  = g_pfg->GetMinorChannel ();
  307.     BOOL    bRet    = FALSE;
  308.     
  309.     switch(message)
  310.     {
  311.     case WM_INITDIALOG:
  312.         {
  313.             //refresh the controls
  314.             SetDlgItemInt (hDlg, IDC_MAJOR_CHANNEL, lChannelMaj, TRUE);
  315.             SetDlgItemInt (hDlg, IDC_MINOR_CHANNEL, lChannelMin, TRUE);
  316.             //set the spin controls
  317.             HWND hWndSpin = GetDlgItem (hDlg, IDC_SPIN_MAJOR);
  318.             ::SendMessage(
  319.                 hWndSpin, 
  320.                 UDM_SETRANGE32, 
  321.                 static_cast <WPARAM> (MINOR_CHANNEL_LOWER_LIMIT),
  322.                 static_cast <LPARAM> (MAJOR_CHANNEL_UPPER_LIMIT)
  323.                 ); 
  324.             hWndSpin = GetDlgItem (hDlg, IDC_SPIN_MINOR);
  325.             ::SendMessage(
  326.                 hWndSpin, 
  327.                 UDM_SETRANGE32, 
  328.                 static_cast <WPARAM> (MINOR_CHANNEL_LOWER_LIMIT), 
  329.                 static_cast <LPARAM> (MINOR_CHANNEL_UPPER_LIMIT)
  330.                 ); 
  331.             break;
  332.         }
  333.     case WM_DESTROY:
  334.         {
  335.             EndDialog (hDlg, 0);
  336.             return TRUE;
  337.         }
  338.     case WM_COMMAND:
  339.         {
  340.             switch(LOWORD(wParam))
  341.             {
  342.             case IDC_ENTER:
  343.                 {
  344.                     lChannelMaj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, TRUE);
  345.                     lChannelMin = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  346.                     g_pfg->ChangeChannel (lChannelMaj, lChannelMin);
  347.                     break;
  348.                 }
  349.             case IDOK:
  350.                 {
  351.                     lChannelMaj = (LONG) GetDlgItemInt(hDlg, IDC_MAJOR_CHANNEL, &bRet, TRUE);
  352.                     lChannelMin = (LONG) GetDlgItemInt(hDlg, IDC_MINOR_CHANNEL, &bRet, TRUE);
  353.                     g_pfg->ChangeChannel (lChannelMaj, lChannelMin);
  354.                 }
  355.             case IDCANCEL:
  356.                 {
  357.                     EndDialog (hDlg, 0);
  358.                     break;
  359.                 }
  360.             }
  361.             break;
  362.         }
  363.     }
  364.     return FALSE;
  365. }
  366.  
  367.  
  368. // ErrorMessageBox
  369. //
  370. // Opens a Message box with a error message in it.  The user can     
  371. // select the OK button to continue.
  372. //
  373. VOID
  374. ErrorMessageBox(LPTSTR sz,...)
  375. {
  376.     static TCHAR    ach[2000];
  377.     va_list         va;
  378.  
  379.     va_start(va, sz);
  380.     wvsprintf(ach, sz, va);
  381.     va_end(va);
  382.  
  383.     MessageBox(hwndMain, ach, NULL, MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
  384. }
  385.  
  386.